package cn.lyvust.eais.manage.controller;

import cn.lyvust.eais.common.annotation.SysLogs;
import cn.lyvust.eais.common.bean.ResponseCode;
import cn.lyvust.eais.common.bean.ResponseResult;
import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.common.util.BeansUtil;
import cn.lyvust.eais.manage.dto.param.DuTestParam;
import cn.lyvust.eais.manage.entity.DuTest;
import cn.lyvust.eais.manage.service.DuTestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  idea里面直接用git提交
 * </p>
 * @author FIENDLMAO
 * @since 2021-02-08
 */
@Api(tags = {"Du招生意向调查"})
@RestController
@Controller
@RequestMapping("/dutest")
public class DuTestController {
    @Autowired
    private DuTestService duTestService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询招生意向")
    public ResponseResult list(@RequestBody DuTestParam param){
        QueryWrapper wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(param.getId())) {
            wrapper.eq("id", param.getId());
        }
        wrapper.orderByDesc("update_time");
        return ResponseResult.e(ResponseCode.OK, duTestService.list(wrapper));
    }
    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增意向信息")
    @SysLogs("新增一个招生意向信息")
    public ResponseResult add(@RequestBody DuTestParam param){
        if (StringUtils.isEmpty(param.getUsername())) {
            throw RequestException.fail("添加失败,姓名不能为空!");
        }
        if (StringUtils.isEmpty(param.getSchool())) {
            throw RequestException.fail("添加失败,毕业学校不能为空!");
        }
        if (StringUtils.isEmpty(param.getMajor())) {
            throw RequestException.fail("添加失败,咨询专业不能为空!");
        }
        if (StringUtils.isEmpty(param.getPhone())) {
            throw RequestException.fail("添加失败,手机号不能为空!");
        }
        DuTest entity = new DuTest();
        BeanUtils.copyProperties(param, entity);
        DuTest one = duTestService.getOne(new QueryWrapper<DuTest>().eq("phone", param.getPhone()));
        if (one != null) {
            throw RequestException.fail("报名信息已提交,无需重复提交!");
        }
        return ResponseResult.e(ResponseCode.OK, duTestService.save(entity) );
    }
    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个招生意向信息")
    @SysLogs("修改一个招生意向信息")
    public ResponseResult update(@RequestBody DuTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,修改操作ID不能为空!");
        }
        DuTest entity = duTestService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, duTestService.updateById(entity));
    }
    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个招生意向信息")
    @SysLogs("删除一个DuTest信息")
    public ResponseResult delete(@RequestBody DuTestParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("删除失败,ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, duTestService.removeById(param.getId()));
    }
}
